package com.imoresoft.magic.action;

import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.sql.dao.impl.SuperDaoImpl;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.imoresoft.magic.property.AppConstants;
import com.imoresoft.magic.util.DateUtil;

public class FeedbackAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Map<String, String> feedback;

	private IDataset feedbacks;

	SuperDao superDao = null;

	public FeedbackAction() throws SQLException {
		superDao = new SuperDaoImpl(pd.getConn());
	}

	public IDataset getFeedbacks() {
		return feedbacks;
	}

	public void setFeedbacks(IDataset feedbacks) {
		this.feedbacks = feedbacks;
	}

	public Map<String, String> getFeedback() {
		return feedback;
	}

	public void setFeedback(Map<String, String> feedback) {
		this.feedback = feedback;
	}

	@SuppressWarnings("unchecked")
	public String addFeedback() throws Exception {
		IData feedabckMap = new DataMap(this.getFeedback());
		feedabckMap.put("FEEDBACK_TIME", pd.getTime());
		feedabckMap.put("NICK", pd.getTopNick());
		feedabckMap.put("IS_REPLY", AppConstants.FLAG_NO);

		superDao.save("TF_M_FEEDBACK", feedabckMap);
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String queryMyFeedbacks() {
		String nick = pd.getTopNick();
		IData param = new DataMap();
		param.put("NICK", nick);
		String sql = "select * from TF_M_FEEDBACK where nick=:NICK"
				+ " order by feedback_id desc";
		try {
			feedbacks = superDao.queryListBySql(sql, param);
		} catch (Exception e) {
			logger.warn("", e);
		}
		return "my_feedback";
	}

	@SuppressWarnings("unchecked")
	public String queryAllUnreplyFeedbacks() {
		IData param = new DataMap();
		String sql = "select * from tf_m_feedback where is_reply = 0";
		try {
			feedbacks = superDao.queryListBySql(sql, param);
		} catch (Exception e) {
			logger.warn("", e);
		}
		return "reply_feedback";
	}

	public String replyFeedback() {
		feedback.put("IS_REPLY", AppConstants.FLAG_YES);
		feedback.put("REPLY_TIME", DateUtil.formatDate(new Date()));
		feedback.put("REPLY_PERSON", pd.getTopNick());
		try {
			superDao.update("TF_M_FEEDBACK", new DataMap(feedback));
		} catch (Exception e) {
			logger.warn("保存回复失败");
		}
		return "success";
	}
}
